<?php

class IndexController extends Zend_Controller_Action
{

    /**
     * FlashMessenger
     *
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('pirates-layout');
        }

        $this->_flashMessenger = new Lib_Flash();
        $auth = Zend_Auth::getInstance();
        $data = $auth->getStorage()->read();
        $massiv = (array)$data;
        if (isset($massiv['name'])) $this->view->name = $massiv['name'];
    }

    public function indexAction()
    {
        $this->view->pageName = "Welcome to TreasureAce.com";
    }

    public function treasuremapAction()
    {
        $this->_helper->layout()->setLayout('pirates2-layout');
    }

    public function discoverAction()
    {
        //               $this->_helper->layout()->setLayout('pirates2-layout');
    }

    public function aboutAction()
    {
        //       $this->_helper->layout()->setLayout('pirates2-layout');
    }

    public function privacyAction()
    {

    }

    public function termsAction()
    {

    }

    public function loginAction()
    {
        $form = new App_Form_User_Login();

        if ($this->_request->isPost()) {
            if ($form->isValid($this->_request->getParams())) {

                $usersManager = new \Managers\Users();
                if ($usersManager->auth($this->_request->getParam('email'), $this->_request->getParam('password'))) {
                    if ($this->_request->getParam('remember')) {
                        $authData = Zend_Auth::getInstance()->getStorage()->read();
                        $usersManager = new \Managers\Users();
                        $credential = $usersManager->getSecretWord($authData->name,
                                                                   $authData->email,
                                                                   md5($this->_request->getParam('password')));
                        $word = sprintf('%s|%s', $authData->id, $credential);
                        setcookie('user', $word, time() + 365 * 24 * 60 * 60, '/');
                    }
                    $this->_redirect('/user');
                } else {
                    $this->_flashMessenger->addMessage('Input valid email and password.', Lib_Flash::ERROR, Lib_Flash::POST);
                }
            }
        }

        $this->view->form = $form->render();
    }

    public function registerAction()
    {
        $form = new App_Form_User_Register();
        $token = $this->_request->getParam('token');

        if(!empty($token)) {
            $inviteManager = new \Managers\Invites();
            try {
                $invite = $inviteManager->getByToken($token);
                $params = array('email' => $invite['email']); //имейл друга для формы
                $form->populate($params);
            } catch(Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
            }
        }

        if ($this->_request->isPost()) {
            if ($form->isValid($this->_request->getParams())) {
                $values = $form->getValues();
                $usersManager = new \Managers\Users();
                $user = $usersManager->add($values);
                $usersManager->auth($values['email'], $values['password']);

                $this->sendEmail($user->getName(), $user->getEmail());
                $this->_flashMessenger->addMessage('Thank you for registration.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                $this->_redirect("/congratulate");
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::NOW);
            }

        }
        $this->view->form = $form->render();
    }
    public function congratulateAction(){
        $auth = Zend_Auth::getInstance();
        $data = $auth->getStorage()->read();
        $massiv = (array)$data;
        
        if (isset($massiv['name']))
        {
        $this->view->redirect = '<meta http-equiv="REFRESH" content="30;url=/user"/>';
        }
        else $this->_redirect("/login");
        }

    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        setcookie('user', null, time());
        $this->_redirect('/login');

    }

    private function sendEmail($name, $email, $subject = 'Thanks for registration.', $from = 'admin@gbcf.biz')
    {
        $body = <<<_END
            <h1>Hello {$name}. </h1>
            <h2>You've been successfully registered.</h2>
_END;
        $mail = new Zend_Mail();
        $mail->setBodyHtml($body);
        $mail->setFrom($from);
        $mail->addTo($email);
        $mail->setSubject($subject);
        $mail->send();

    }

    public function pageAction()
    {
        $id = $this->_getParam("id");
        $PM = new \Managers\Statics();
        $page = $PM->getPage($id);
        $this->view->page = $page[0];
        $this->view->pageName = $page[0]["menuPageName"];

    }
    public  function  contactusAction()
    {


         $this->view->status=$this->_getParam('send');
      if ($this->_request->isPost()) {


               
                $name =$this->_getParam('name-guest');
                $email =$this->_getParam('email-guest');
                $text =$this->_getParam('text-guest');

/*for merge*/
              $mailManager=new \Managers\Mail();
              $mailManager->send($name,$email,$text);
               $this->_redirect(Zend_Registry::get('config')->resources->frontController->baseUrl."/index/contactus/send/1");
      }
    }

}
